package cfca.paperless.client.servlet.performance;

import java.io.File;
import java.util.Date;

import org.apache.commons.io.FileUtils;

import cfca.paperless.client.util.GUID;
import cfca.paperless.client.util.IoUtil;
import cfca.sadk.lib.crypto.JCrypto;
import cfca.sadk.lib.crypto.Session;
import cfca.sadk.util.Base64;
import cfca.seal.sadk.DonePdfSeal;
import cfca.seal.sadk.PrePdfReader;
import cfca.seal.sadk.PrePdfSeal;
import cfca.seal.sadk.PrePdfSealExtra;

public class testSealAutoPdfPerformance2 {

    public static void main(String[] args) throws Exception {
        try {
            long timeStart = System.currentTimeMillis();

            byte[] pdf = FileUtils.readFileToByteArray(new File("./TestData/autoCalculatePdfHash.pdf"));
            //byte[] pdf = FileUtils.readFileToByteArray(new File("./TestData/compoundSealAutoPdfListDetached-2.pdf"));

            String sealDataPath = "./TestData/seal.seal";
            
            byte[] sealFileBytes = FileUtils.readFileToByteArray(new File(sealDataPath));
            
            JCrypto.getInstance().initialize(JCrypto.JSOFT_LIB, null);
            Session session = JCrypto.getInstance().openSession(JCrypto.JSOFT_LIB);
            PrePdfSeal prePdfSeal = new PrePdfSeal(Base64.decode(sealFileBytes), "11111111", 0.68f, PrePdfSeal.EncryptionAlgorithm.RSA, PrePdfSeal.HashAlgorithm.SHA1);
            
            long startTime = System.currentTimeMillis();
            for (int i = 0; i < 1; i++) {
                new SealPerformance2(pdf, prePdfSeal, startTime).start();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

}

class SealPerformance2 extends Thread {
    static int successCount;
    static int failueCount;
    static long startTime;

    private synchronized static void successAdd() {
        successCount++;
    }

    private synchronized static void failueAdd() {
        failueCount++;
    }

    PrePdfSeal prePdfSeal;
    byte[] pdf;

    public SealPerformance2(byte[] pdf, PrePdfSeal prePdfSeal, long startTime) {
        super();
        this.pdf = pdf;
        this.prePdfSeal = prePdfSeal;
        this.startTime = startTime;
    }

    @Override
    public void run() {
        long timeStart = System.currentTimeMillis();
        boolean f = process(pdf, prePdfSeal);
        System.out.println("time:" + ":" + (System.currentTimeMillis() - timeStart));
    }

    private boolean process(byte[] pdf, PrePdfSeal prePdfSeal) {
        try {
            for (int i = 0; i < 500; i++) {

                DonePdfSeal donePdfSeal = new DonePdfSeal();
                PrePdfReader prePdfReader = new PrePdfReader(pdf);
                donePdfSeal.initPdfReader(prePdfReader);
                donePdfSeal.initPdfSeal(prePdfSeal);
                PrePdfSealExtra prePdfSealExtra = new PrePdfSealExtra("", "");
                donePdfSeal.initPdfSealExtra(prePdfSealExtra);
                donePdfSeal.updateLocationInfoByCoordinate(1, 100, 400);
                
                byte[] sealedPdf = donePdfSeal.createPdfSeal(0);
                
                SealPerformance2.successAdd();
                String savedPdfId = GUID.generateId();
                IoUtil.write("./TestData/performance/" + savedPdfId + ".pdf", sealedPdf);
                if(SealPerformance2.successCount % 100 == 0) {
                    long period = (System.currentTimeMillis() - startTime)/1000;
                    long tps = SealPerformance2.successCount / period;
                    System.out.println("tps:" + tps + ",period:" + period);
                }
//                Thread.sleep(100);
            }
            return true;
        } catch (Exception e) {
            return false;
        }
    }

}
